// pages/order/pay/index.js

const app = getApp();
let api = require('../../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payway: 1,
    money: 1500,
    orderId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    var that = this;
    var orderId = options.orderid;
    console.log(orderId);
    var money = options.money;
    that.setData({
      money: money,
      orderId: orderId
    })
  },

  switch1Change: function (e) {
    const that = this;
    that.setData({
      payway: e.detail.value
    })
    // console.log('radio发生change事件，携带value值为：', e.detail.value)
  },
  // 现金支付
  sendDriver: function () {

    var token = wx.getStorageSync('TOKEN')
    var param = {
      "bizType": 1,
      "id": this.data.orderId,
      "payType": 2
    }
    api._post('/pay/action', param, token).then(res => {
      // console.log(res);
      wx.switchTab({
        url: '/pages/order/order',
      });
    })
  },
  // 微信支付
  wechatPay: function () {
    var token = wx.getStorageSync('TOKEN')
    var param = {
      "bizType": 1,
      "id": this.data.orderId,
      "payType": 1
    }
    console.log(param);
    api._post('/pay/action', param, token).then(resulet => {
      var data = resulet.result
      console.log(data,'这是data数据');
      console.log(resulet,'这是result数据');
      wx.requestPayment({
        timeStamp: data.timeStamp, // 时间戳，必填（后台传回）
        nonceStr: data.nonceStr, // 随机字符串，必填（后台传回）
        package: data.package, // 统一下单接口返回的 prepay_id 参数值，必填（后台传回）
        signType: data.signType, // 签名算法，非必填，（预先约定或者后台传回）
        paySign: data.paySign, // 签名 ，必填 （后台传回）
        success: function (res) { // 成功后的回调函数
          console.log('成功');
          wx.switchTab({
            url: '/pages/order/order',
          });
        },
        fail: function (res) {
          console.log('支付失败');
          wx.switchTab({
            url: '/pages/order/order',
          });

        }
      })
    })

  }
})